package com.powernobug.servlet;

import com.powernobug.bean.model.MarketTopic;
import com.powernobug.bean.vo.BaseRespVo;
import com.powernobug.bean.vo.PageVo;
import com.powernobug.bean.vo.param.TopicQueryParam;
import com.powernobug.service.AdminCouponService;
import com.powernobug.service.impl.AdminCouponServiceImpl;
import com.powernobug.service.AdminTopicService;
import com.powernobug.service.impl.AdminTopicServiceIml;
import com.powernobug.util.JsonUtil;
import com.powernobug.util.BeanUtil;
import com.powernobug.util.JacksonUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Map;


/**
 * @description:
 * @project: project1-no-bug
 * @package: com.powernobug.servlet
 * @author: HuangWeiLong
 * @date: 2024/8/23 14:53
 */
@WebServlet("/topic/*")
public class AdminTopicServlet extends CommonServlet{
    static {
        DateConverter dateConverter = new DateConverter(null);
        dateConverter.setPatterns(new String[] {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"});
        ConvertUtils.register(dateConverter, Date.class);
    }
    AdminTopicService adminTopicService=new AdminTopicServiceIml();
    public BaseRespVo list(HttpServletRequest request){
        TopicQueryParam topicQueryParam = BeanUtil.queryParamPagemant(TopicQueryParam.class, request);
        PageVo data=adminTopicService.list(topicQueryParam);
        return BaseRespVo.ok(data);
    }
    public BaseRespVo create(HttpServletRequest request) throws IOException, ServletException {
        String json = request.getReader().readLine();
        Map map = JacksonUtil.read(json, Map.class);
        MarketTopic marketTopic = new MarketTopic();
        try {
            BeanUtils.copyProperties(marketTopic,map);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        return adminTopicService.create(marketTopic);
    }
    public BaseRespVo update(HttpServletRequest request) throws IOException {
        String jrson = request.getReader().readLine();
        Map map = JacksonUtil.read(jrson, Map.class);

        MarketTopic marketTopic = new MarketTopic();
        try {
            BeanUtils.copyProperties(marketTopic,map);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        return adminTopicService.update(marketTopic);
    }

    public BaseRespVo delete(HttpServletRequest request) throws IOException {
        String jrson = request.getReader().readLine();
        Map map = JacksonUtil.read(jrson, Map.class);

        MarketTopic marketTopic = new MarketTopic();
        try {
            BeanUtils.copyProperties(marketTopic,map);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        adminTopicService.delete(marketTopic);
        return BaseRespVo.ok(null);
    }
}
